﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
using System.Configuration;

namespace OCR
{
    public partial class UPictureConfig : UserControl
    {

        //当前选择的配置方案
        public OcrConfigInfo curOcrInfo;
        //新增？
        private bool isAdd;

        private Main main;

        public UPictureConfig(Main main)
        {
            this.main = main;
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            this.main.SetEnabel(true);
            this.curOcrInfo.OperatorsName = this.operatorsName.Text;
            this.curOcrInfo.FaceValue =this.faceValue.Text;
            this.curOcrInfo.Font = this.cbFont.Text;
            this.curOcrInfo.RecognitionNum = this.recognitionNum.Checked;
            this.curOcrInfo.RecognitionChar = this.recognitionEng.Checked;
            this.curOcrInfo.PwLeft = (int)this.pwLeft.Value;
            this.curOcrInfo.PwTop = (int)this.pwTop.Value;
            this.curOcrInfo.PwWidth = (int)this.pwWidth.Value;
            this.curOcrInfo.PwHeight = (int)this.pwHeight.Value;
            this.curOcrInfo.NoLeft = (int)this.noLeft.Value;
            this.curOcrInfo.NoTop = (int)this.noTop.Value;
            this.curOcrInfo.NoWidth = (int)this.noWidth.Value;
            this.curOcrInfo.NoHeight = (int)this.noHeight.Value;
            this.curOcrInfo.PwCount = (int)this.pwCount.Value;
            this.curOcrInfo.NoCount = (int)this.noCount.Value;
            this.curOcrInfo.ValidDate = this.validDate.Value;
            if (this.isAdd)
            {
                this.curOcrInfo.ConfigName = this.newConfigSchemeName.Text;
                if (Main.pictureConfigList == null)
                {
                    Main.pictureConfigList = new List<OcrConfigInfo>();
                }

                if (Main.pictureConfigList.Find(d => d.ConfigName == this.curOcrInfo.ConfigName) != null)
                {
                    MessageBox.Show("新方案名称重复！");
                    this.newConfigSchemeName.Focus();
                }
                else
                {
                    this.cbConfigSchemeName.Enabled = true;
                    Main.pictureConfigList.Add(this.curOcrInfo);
                    this.btnDefualt.Enabled = true;
                    this.btnDel.Enabled = true;
                    this.cbConfigSchemeName.Enabled = true;
                    try
                    {
                        string orcJson = JsonConvert.SerializeObject(Main.pictureConfigList);
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(Main.pictureConfigPath, false))
                        {
                            file.WriteLine(orcJson);
                        }
                        this.cbConfigSchemeName.Items.Add(this.curOcrInfo.ConfigName);
                        this.cbConfigSchemeName.SelectedItem = this.curOcrInfo.ConfigName;
                    }
                    catch
                    {

                    }
                    this.btnDel.Enabled = true;
                    this.isAdd = false;
                }
            }
            else
            {
                this.cbConfigSchemeName.Enabled = true;
                this.btnDefualt.Enabled = true;
                this.btnDel.Enabled = true;
                this.cbConfigSchemeName.Enabled = true;
                try
                {
                    string orcJson = JsonConvert.SerializeObject(Main.pictureConfigList);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Main.pictureConfigPath, false))
                    {
                        file.WriteLine(orcJson);
                    }
                }
                catch
                {

                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.isAdd = true;
            this.curOcrInfo = new OcrConfigInfo();
            this.btnDefualt.Enabled = false;
            this.label34.Text = "新方案名称";
            this.btnDel.Enabled = false;
            this.btnSave.Enabled = true;
            this.panel1.Enabled = true;
            this.cbConfigSchemeName.SelectedItem = null;
            //this.button5.Enabled = true;
            this.cbConfigSchemeName.Enabled = false;
        }

        private void cbConfigSchemeName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbConfigSchemeName.SelectedItem != null)
            {
                this.btnDefualt.Enabled = true;
                this.btnSave.Enabled = true;
                this.label34.Text = "方案名称";

                this.curOcrInfo = Main.pictureConfigList.Find(d => d.ConfigName == this.cbConfigSchemeName.SelectedItem.ToString());
                try
                {
                    //给控件赋值
                    #region
                    this.faceValue.Text = this.curOcrInfo.FaceValue.ToString();
                    this.cbFont.Text = this.curOcrInfo.Font;
                    this.recognitionNum.Checked = this.curOcrInfo.RecognitionNum;
                    this.recognitionEng.Checked = this.curOcrInfo.RecognitionChar;
                    this.pwLeft.Value = this.curOcrInfo.PwLeft;
                    this.pwTop.Value = this.curOcrInfo.PwTop;
                    this.pwWidth.Value = this.curOcrInfo.PwWidth;
                    this.pwHeight.Value = this.curOcrInfo.PwHeight;
                    this.noLeft.Value = this.curOcrInfo.NoLeft;
                    this.noTop.Value = this.curOcrInfo.NoTop;
                    this.noWidth.Value = this.curOcrInfo.NoWidth;
                    this.noHeight.Value = this.curOcrInfo.NoHeight;
                    this.pwCount.Value = this.curOcrInfo.PwCount;
                    this.noCount.Value = this.curOcrInfo.NoCount;
                    this.validDate.Value = this.curOcrInfo.ValidDate;
                    this.operatorsName.Text = this.curOcrInfo.OperatorsName;
                    this.newConfigSchemeName.Text = this.cbConfigSchemeName.SelectedItem.ToString();
                    this.main.language = this.cbFont.Text;

                    string defaultNum = "";
                    string defaultStr = "";
                    if (this.recognitionNum.Checked)
                    {
                        defaultNum = "0123456789";
                    }
                    else
                    {
                        defaultNum = "";
                    }
                    if (this.recognitionEng.Checked)
                    {
                        defaultStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

                    }
                    else
                    {
                        defaultStr = "";
                    }
                    this.main.defaultList = defaultNum + defaultStr;
                    #endregion


                    //配置方案更改后，程序默认值保存到本地
                    Properties.Settings.Default.ConfigSchemeName = this.cbConfigSchemeName.SelectedItem.ToString();
                    Properties.Settings.Default.Save();
                }
                catch
                {

                }
            }

        }

        private void UPictureConfig_Load(object sender, EventArgs e)
        {
            string tessdata = System.Configuration.ConfigurationManager.AppSettings["tessdata"].ToString();
            string[] tessdatas = tessdata.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            this.cbFont.Items.AddRange(tessdatas);
            this.cbFont.SelectedIndex = 0;
            this.btnDefualt.Enabled = false;
            //this.label34.Visible = false ;
            //this.newConfigSchemeName.Visible = false;

        }

        private void btnDefualt_Click(object sender, EventArgs e)
        {
            this.faceValue.Text = this.curOcrInfo.FaceValue.ToString();
            this.cbFont.Text = this.curOcrInfo.Font;
            this.recognitionNum.Checked = this.curOcrInfo.RecognitionNum;
            this.recognitionEng.Checked = this.curOcrInfo.RecognitionChar;
            this.pwLeft.Value = this.curOcrInfo.PwLeft;
            this.pwTop.Value = this.curOcrInfo.PwTop;
            this.pwWidth.Value = this.curOcrInfo.PwWidth;
            this.pwHeight.Value = this.curOcrInfo.PwHeight;
            this.noLeft.Value = this.curOcrInfo.NoLeft;
            this.noTop.Value = this.curOcrInfo.NoTop;
            this.noWidth.Value = this.curOcrInfo.NoWidth;
            this.noHeight.Value = this.curOcrInfo.NoHeight;
            this.pwCount.Value = this.curOcrInfo.PwCount;
            this.noCount.Value = this.curOcrInfo.NoCount;
            this.validDate.Value = this.curOcrInfo.ValidDate;
            //this.grayLevel.Checked = this.curOcrInfo.GrayLevel;
            //this.twoValue.Checked = this.curOcrInfo.TwoValue;
        }

        private void pwLeft_ValueChanged(object sender, EventArgs e)
        {
            this.pwLeft.Value = (int)this.pwLeft.Value;
            this.main.DrawRectangle();
        }

        private void pwTop_ValueChanged(object sender, EventArgs e)
        {
            this.pwTop.Value = (int)this.pwTop.Value;
            this.main.DrawRectangle();
        }

        private void pwWidth_ValueChanged(object sender, EventArgs e)
        {
            this.pwWidth.Value = (int)this.pwWidth.Value;
            this.main.DrawRectangle();

        }

        private void pwHeight_ValueChanged(object sender, EventArgs e)
        {
            this.pwHeight.Value = (int)this.pwHeight.Value;
            this.main.DrawRectangle();
        }

        private void noLeft_ValueChanged(object sender, EventArgs e)
        {
            this.noLeft.Value = (int)this.noLeft.Value;
            this.main.DrawRectangle();
        }

        private void noTop_ValueChanged(object sender, EventArgs e)
        {
            this.noTop.Value = (int)this.noTop.Value;
            this.main.DrawRectangle();
        }

        private void noWidth_ValueChanged(object sender, EventArgs e)
        {
            this.noWidth.Value = (int)this.noWidth.Value;
            this.main.DrawRectangle();
        }

        private void noHeight_ValueChanged(object sender, EventArgs e)
        {
            this.noHeight.Value = (int)this.noHeight.Value;
            this.main.DrawRectangle();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                Main.pictureConfigList.Remove(curOcrInfo);
                try
                {
                    string orcJson = JsonConvert.SerializeObject(Main.pictureConfigList);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Main.pictureConfigPath, false))
                    {
                        file.WriteLine(orcJson);
                    }
                    this.cbConfigSchemeName.Items.Remove(curOcrInfo.ConfigName);
                    if (Main.pictureConfigList.Count > 0)
                    {
                        this.cbConfigSchemeName.SelectedItem = Main.pictureConfigList[0].ConfigName;
                    }
                    else
                    {
                        this.cbConfigSchemeName.Enabled = false;
                        this.btnSave.Enabled = false;
                        this.btnDel.Enabled = false;
                        this.btnDefualt.Enabled = false;
                        this.cbConfigSchemeName.Text = "配置方案选择";
                        this.main.SetEnabel(false);
                    }
                }
                catch
                {

                }
            }
        }


        private void btnTrain_Click(object sender, EventArgs e)
        {
            Train frm = new Train();
            frm.Show();
        }

        private void btNoRectange_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btNoLeft": this.noLeft.Value--; break;
                case "btNoRight": this.noLeft.Value++; break;
                case "btNoUp": this.noTop.Value--; break;
                case "btNoDown": this.noTop.Value++; break;
                case "btNoLong": this.noWidth.Value++; break;
                case "btNoShort": this.noWidth.Value--; break;
                case "btNoHigh": this.noHeight.Value++; break;
                case "btNoLow": this.noHeight.Value--; break;
                default:
                    break;
            }
            this.main.DrawRectangle();
        }

        private void btPwRectange_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btPwLeft": this.pwLeft.Value--; break;
                case "btPwRight": this.pwLeft.Value++; break;
                case "btPwUp": this.pwTop.Value--; break;
                case "btPwDown": this.pwTop.Value++; break;
                case "btPwLong": this.pwWidth.Value++; break;
                case "btPwShort": this.pwWidth.Value--; break;
                case "btPwHigh": this.pwHeight.Value++; break;
                case "btPwLow": this.pwHeight.Value--; break;
                default:
                    break;
            }
            this.main.DrawRectangle();
        }

        private void cbFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.main.language = this.cbFont.Text;
        }

        private void recognitionEng_CheckedChanged(object sender, EventArgs e)
        {
            string defaultNum = "";
            string defaultStr = "";
            if (this.recognitionNum.Checked)
            {
                defaultNum = "0123456789";
            }
            else
            {
                defaultNum = "";
            }
            if (this.recognitionEng.Checked)
            {
                defaultStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

            }
            else
            {
                defaultStr = "";
            }
            this.main.defaultList = defaultNum + defaultStr;
        }

        private void recognitionNum_CheckedChanged(object sender, EventArgs e)
        {
            string defaultNum = "";
            string defaultStr = "";
            if (this.recognitionNum.Checked)
            {
                defaultNum = "0123456789";
            }
            else
            {
                defaultNum = "";
            }
            if (this.recognitionEng.Checked)
            {
                defaultStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

            }
            else
            {
                defaultStr = "";
            }
            this.main.defaultList = defaultNum + defaultStr;
        }
    }
}
